﻿using QuanLyTiemVang.Classes;
using QuanLyTiemVang.DAO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace QuanLyTiemVang.GUI
{
    /// <summary>
    /// Interaction logic for TraCuuSanPham.xaml
    /// </summary>
    public partial class TraCuuSanPham : Window, IPhanQuyenHelper
    {
        public TraCuuSanPham()
        {
            InitializeComponent();
        }

        private List<TextBox> listTextBox;
        private List<RadioButton> listRadioButton;
        private SANPHAM sp;

        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            CurrentUser.Instance.AddForm(this);
            sp = new SANPHAM();

            // get from database
            comboLoaiSanPham.ItemsSource = SanPhamDAO.GetAllLoaiSanPham();
            dgDanhSachSanPham.ItemsSource = SanPhamDAO.GetAllSanPham();

            listTextBox = new List<TextBox>();
            listRadioButton = new List<RadioButton>();

            listTextBox.Add(txtMaSanPham);
            listTextBox.Add(txtTenSanPham);
            listTextBox.Add(txtTu);
            listTextBox.Add(txtDen);
            this.DisableAllControls();

            listRadioButton.Add(rbMaSanPham);
            listRadioButton.Add(rbTenSanPham);
            listRadioButton.Add(rbLoaiSanPham);
            listRadioButton.Add(rbSoLuongTon);

            this.rbMaSanPham.IsChecked = true;
            this.txtMaSanPham.IsEnabled = true;
        }
        private void DisableAllControls()
        {
            foreach (var item in listTextBox)
            {
                item.IsEnabled = false;
                item.Clear();
            }
            comboLoaiSanPham.IsEnabled = false;
            comboLoaiSanPham.SelectedIndex = -1;
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);

            // Begin dragging the window
            this.DragMove();
        }
        private void RadioButton_Checked_Event_Handler(object sender, RoutedEventArgs e)
        {
            RadioButton rb = sender as RadioButton;
            int index = listRadioButton.IndexOf(rb);

            if (index != -1)
            {
                this.DisableAllControls();
                switch (index)
                {
                    case 0:
                        txtMaSanPham.IsEnabled = true;
                        break;
                    case 1:
                        txtTenSanPham.IsEnabled = true;
                        break;
                    case 2:
                        comboLoaiSanPham.IsEnabled = true;
                        break;
                    case 3:
                        txtTu.IsEnabled = true;
                        txtDen.IsEnabled = true;
                        break;
                    default:
                        break;
                }
            }
        }

        // textbox number only
        private void textBox_NumberOnly_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            CheckIsNumeric(e);
        }

        private void CheckIsNumeric(TextCompositionEventArgs e)
        {
            int result;
            if (!(int.TryParse(e.Text, out result)))
                e.Handled = true;
        }

        private void btnThoat_Click_1(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void TraCuu_Button_Click_EventHandler(object sender, RoutedEventArgs e)
        {
            // get information
            if (Decimal.Parse(txtDen.Text.ToString()) < Decimal.Parse(txtTu.Text.ToString()))
            {
                MessageBox.Show("Khoảng số lượng tồn sai.", "Lỗi", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            int i = checkValid();
            if (i != -1)
            {
                switch (i)
                {
                    case 0:
                        sp.MaSanPham = Int32.Parse(txtMaSanPham.Text);
                        dgDanhSachSanPham.ItemsSource = SanPhamDAO.SelectSanPhamLikeMaSanPham(sp);
                        break;
                    case 1:
                        sp.TenSanPham = txtTenSanPham.Text;
                        dgDanhSachSanPham.ItemsSource = SanPhamDAO.SelectSanPhamLikeTenSanPham(sp);
                        break;
                    case 2:
                    case 3:
                        int x, y;
                        bool bMin = Int32.TryParse(txtTu.Text, out x);
                        bool bMax = Int32.TryParse(txtDen.Text, out y);
                        dgDanhSachSanPham.ItemsSource = SanPhamDAO.SelectSanPhamBySoLuongTon(bMin == true ? x : -1, bMax == true ? y : -1);
                        break;
                    case 4:
                        LOAISANPHAM loai = (LOAISANPHAM)comboLoaiSanPham.SelectedItem;
                        sp.MaLoaiSanPham = loai.MaLoaiSanPham;
                        dgDanhSachSanPham.ItemsSource = SanPhamDAO.SelectSanPhamLikeLoaiSanPham(sp);
                        break;
                }
                if (dgDanhSachSanPham.ItemsSource == null)
                    MessageBox.Show("Không tìm thấy dữ liệu phù hợp.", "Thông báo", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            else
            {
                dgDanhSachSanPham.ItemsSource = SanPhamDAO.GetAllSanPham();
            }
        }

        private int checkValid()
        {
            if (comboLoaiSanPham.IsEnabled)
            {
                if (comboLoaiSanPham.SelectedIndex == -1)
                {
                    return -1;
                }
                else
                {
                    return 4;
                }
            }
            else
            {
                foreach (var item in listTextBox)
                {
                    if (string.IsNullOrEmpty(item.Text) == false)
                    {
                        return listTextBox.IndexOf(item);
                    }
                }

                return -1;
            }
        }

        void IPhanQuyenHelper.SetAdminFunction()
        {
        }

        void IPhanQuyenHelper.SetStaffFunction()
        {
        }

        void IPhanQuyenHelper.SetCustomerFunction()
        {
        }

        void IPhanQuyenHelper.SetNonFunction()
        {
            this.Close();
        }
    }
}
